package com.jade.zsy.base.facade.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.base.utils.MsgUtil;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.base.common.StatusConstant;
import com.jade.zsy.base.dao.PositionDao;
import com.jade.zsy.base.entity.Position;
import com.jade.zsy.base.facade.PositionFacade;
import com.jade.zsy.base.vo.PositionVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/5/31
 * Time: 11:07
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@Service("PositionFacade")
public class PositionFacadeImpl extends BaseFacadeImpl implements PositionFacade {
    @Inject
    @Named("PositionDao")
    private PositionDao entityDao;

    @Override
    @Transactional
    public void save(Object obj) throws DbException {
        Position entity=(Position)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setOwnerId(customer.getOwnerId());
        entity.setCreator(customer.getNickName());
        entity.setCreatorId(customer.getId());
        entity.setStatus(StatusConstant.RECORD_NORMAL);
        super.save(entity);
    }
    @Override
    @Transactional
    public void update(Object obj) throws DbException {
        Position entity=(Position)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setModifier(customer.getTrueName());
        super.update(entity);
    }
    @Override
    @Transactional
    public void delete(long... obj) throws DbException {
        for(long id : obj){
            Position org=entityDao.getById(Position.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            entityDao.delete(Position.class,org);
        }
    }
    @Transactional
    public void disabled(long[] obj) throws DbException {
        for(long id : obj){
            Position org=entityDao.getById(Position.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            org.setStatus(StatusConstant.RECORD_STOP);
            entityDao.update(org);
        }
    }

    @Transactional
    public void enabled(long[] obj) throws DbException {
        for(long id : obj){
            Position enOrg=entityDao.getById(Position.class,id);
            if(enOrg==null || enOrg.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            enOrg.setStatus(StatusConstant.RECORD_NORMAL);
            entityDao.update(enOrg);
        }
    }

    @Override
    public PagingResult queryEntity(Object obj, int page, int size, String keys, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        PositionVo entity=( PositionVo)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        Criteria criteria = new Criteria((page - 1) * size, size);
        criteria.add(Restrictions.eq("ownerId",customer.getOwnerId()));

        if(StringUtils.isNotEmpty(keys)){
            criteria.add(Restrictions.or(Restrictions.like("code",keys),Restrictions.eq("name",keys)));
        }
        if(entity!=null && StringUtils.isNotEmpty(entity.getCode())){
            criteria.add(Restrictions.like("code",entity.getCode()));
        }
        if(entity!=null && entity.getStatus()>0){
            criteria.add(Restrictions.eq("status",entity.getStatus()));
        }
        if(entity!=null && StringUtils.isNotEmpty(entity.getName())){
            criteria.add(Restrictions.like("name",entity.getName()));
        }
        PagingList pagingList = entityDao.queryEntity( Position.class, criteria);
        PagingResult  result = PagingResult.getResult(CopyBean.copy(pagingList, PositionVo.class), pagingList.getTotalCount(), page, size);
        return result;
    }
}
